<template>
  <div class="main-wrapper">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="标题" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.categoryId" placeholder="类型" clearable class="filter-item">
        <el-option v-for="item in categoryList" :key="item.id" :label="item.value" :value="item.id" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <!-- <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button> -->
      <router-link to="/case/create"><el-button class="filter-item" type="primary" icon="el-icon-edit">添加</el-button></router-link>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row>
      <el-table-column label="标题" align="center">
        <template slot-scope="{row}">
          {{ row.title }}
        </template>
      </el-table-column>
      <el-table-column label="作者" align="center">
        <template slot-scope="{row}">
          {{ row.author }}
        </template>
      </el-table-column>
      <el-table-column label="封面图" width="160" align="center">
        <template slot-scope="{row}">
          <el-image :src="getFullImageUrl(row.imgUrl)" />
        </template>
      </el-table-column>
      <el-table-column label="摘要信息" align="center">
        <template slot-scope="{row}">
          {{ row.summary }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="80" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.status | styleFilter()">{{ row.status | statusFilter() }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="132" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="212">
        <template slot-scope="{row}">
          <router-link :to="'/case/detail/'+row.id"><el-button size="mini" type="success">详情</el-button></router-link>
          <router-link :to="'/case/edit/'+row.id"><el-button type="primary" size="mini">修改</el-button></router-link>
          <el-button v-if="row.status===0" size="mini" type="success" @click="publishProjectCase(row)">发布</el-button>
          <el-button v-if="row.status!==2" size="mini" type="warning" @click="delProjectCase(row)">删除</el-button>
          <el-button v-if="row.status===2" size="mini" @click="reuseProjectCase(row)">恢复</el-button>
          <el-button size="mini" type="danger" @click="removeCase(row)">彻底删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchProjectCaseList" />
  </div>
</template>

<script>
import { getProjectCaseList, editProjectCaseStatus, removeProjectCase } from '@/api/projectCase'
import { getImageUrl, parseTime } from '@/utils'
import { getProjectConfigList } from '@/api/config'
import Pagination from '@/components/Pagination'

export default {
  name: 'ProjectCase',
  components: {
    Pagination
  },
  filters: {
    styleFilter(style) {
      const styleMap = { 0: '', 1: 'success', 2: 'danger' }
      return styleMap[style]
    },
    timeFilter(time, format) {
      return parseTime(time, format)
    },
    statusFilter(value) {
      const statusMap = { 0: '草稿', 1: '已发布', 2: '废弃' }
      return statusMap[value]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      categoryList: [],
      projectCaseData: {},
      dialogFormVisible: false,
      dialogDetailVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '添加'
      },
      rules: {}
    }
  },
  created() {
    this.fetchCategoryList()
    this.fetchProjectCaseList()
  },
  methods: {
    fetchCategoryList() {
      getProjectConfigList(1004).then(res => {
        this.categoryList = res.data
      })
    },
    fetchProjectCaseList() {
      this.listLoading = true
      getProjectCaseList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchProjectCaseList()
    },
    resetProjectCaseData() {
      this.projectCaseData = {}
    },
    removeCase(row) {
      removeProjectCase(row.id).then(res => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.fetchProjectCaseList()
      })
    },
    handleCreate() {
      this.resetProjectCaseData()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    delProjectCase(row) {
      row.status = 2
      this.updateProjectCaseStatus(row, 2)
    },
    reuseProjectCase(row) {
      row.status = 1
      this.updateProjectCaseStatus(row, 0)
    },
    publishProjectCase(row) {
      row.status = 1
      this.updateProjectCaseStatus(row, 1)
    },
    updateProjectCaseStatus(row, status) {
      row.status = status
      editProjectCaseStatus(row.id, status).then(() => {
        this.projectCaseData = Object.assign({}, row)
        this.projectCaseData.status = status
        for (const v of this.list) {
          if (v.id === this.projectCaseData.id) {
            const index = this.list.indexOf(v)
            this.list.splice(index, 1, this.projectCaseData)
            break
          }
        }
      })
      this.$message({
        message: '操作成功',
        type: 'success'
      })
    },
    getFullImageUrl(name) {
      return getImageUrl(name)
    }
  }
}
</script>
<style lang="scss" scoped>
.main-wrapper {
  margin: 8px;
  .filter-container {
    padding: 10px 0;
    .filter-item {
      width: 200px;
      margin-right: 10px;
    }
  }
}
</style>
